ARCH=arm64
CROSS_COMPILE=aarch64-linux-gnu-
LINUXVERSION=v5.15

include ../../Makefile.inc
# Makefile for Ampere Jade.
# This is only partially done. It is derived from other ARM
# systems. 
# The goal is to create a kernel we can start from the UEFI shell. At present,
# when we do this, we get this:
# FS3:\EFI\opensuse\> Image
# EFI stub: Booting Linux Kernel...
# EFI stub: Generating empty DTB
# EFI stub: Exiting boot services and installing virtual address map...

default: flashkernel

flash: tiny.bin

flashtest: testflashkernel

usefultargets:
	echo fetch, uroot, kernel, or image.bin

flashinitramfs.cpio.lzma: flashinitramfs.cpio
	lzma -f -k $<

flashinitramfs.cpio: Makefile
	cd u-root && go build . && GOARCH=arm64 ./u-root -uinitcmd="boot" -o $@ \
		./cmds/core/* \
		./cmds/boot/* \
		./cmds/exp/systemboot \
		./cmds/exp/localboot \
		./cmds/exp/fbnetboot \
		./cmds/exp/dmidecode \
		./cmds/exp/acpicat \
		./cmds/exp/acpigrep \
		./cmds/exp/disk_unlock \
		./cmds/exp/dmidecode \
		./cmds/exp/ipmidump \
		../cpu/cmds/cpud
	cp u-root/flashinitramfs.cpio .

# this target builds an initramfs with only one program, the cpu server.
# It depends on the kernel setting the IP address.
# You need ip=dhcp OR ip=fixed.ip.address.here in the command line
cpu.cpio.lzma: Makefile
	GO111MODULE=off GOARCH=arm64 go run github.com/u-root/u-root -o cpu.cpio -build=bb -initcmd=cpud -files ~/.ssh/cpu_rsa.pub:key.pub \
		-defaultsh="" \
		github.com/u-root/cpu/cmds/cpud
	lzma -f -k cpu.cpio

# this target builds an initramfs with all of u-root, AND all of /bin, /usr/bin, /sbin, /usr/sbin, /etc
# the intent is  to demonstrate the capability of http loading.
# It depends on the kernel setting the IP address.
# You need ip=dhcp OR ip=fixed.ip.address.here in the command line
sshd.cpio.lzma:  Makefile
	GO111MODULE=off GOARCH=arm64 go run github.com/u-root/u-root -o sshd.cpio -build=bb \
		-uinitcmd=/bbin/sshd \
		-files class_key.pub:authorized_keys \
		-files classhostkey:id_rsa \
		-files /usr/bin/vi \
		-files /usr/share/vim \
		-files /usr/share/terminfo \
		-files /bin/bash \
		-files /usr/sbin/flashrom \
		-files /usr/bin/xz \
		all
	lzma -f -k sshd.cpio
	ls -l sshd.cpio.*

flashkernel: flash.config flashinitramfs.cpio.lzma Makefile
	cp $< linux/.config
	(cd linux && git apply ../0001-efistub-Workaround-Linuxboot-for-Ubuntu-20.04.patch)
	(cd linux && ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- make olddefconfig && ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- make -j32)
	cp linux/arch/arm64/boot/Image $@

testflashkernel: flashkernel
	qemu-system-aarch64 -machine virt -cpu cortex-a57 -kernel flashkernel -nographic -initrd flashinitramfs.cpio.lzma -append "console=ttyAMA0,115200 keep_bootcon"

